<template>
  <div class="app-container">
    <el-form v-if="rolesKey == '00'" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户手机号码" label-width="100px" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入商户手机号码"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="商户状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-form v-else :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="组名" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="组名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8"  v-if="rolesKey != '00'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resource:group:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resource:group:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-if="rolesKey == '00'" v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="商家头像" align="center" key="pic" width="110px" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic"
                    style="width: 100px; height: 100px"
                    fit="cover"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="商家名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="负责人" align="center" key="leader	" prop="leader"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="代理商名称	" align="center" key="proxyName" prop="proxyName"
                       width="120"/>
      <el-table-column label="商户状态" align="center" key="status"  >
        <template slot-scope="scope">
          <span style="color: #3894ff" v-if="scope.row.status == 0">正常</span>
          <span style="color: #ed5565" v-if="scope.row.status == 1">停用</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            @click="handleList(scope.row)"
            v-hasPermi="['resource:group:list']"
          >查看商户资源分组
          </el-button>
        </template>

      </el-table-column>
    </el-table>


    <el-table v-else v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="资源名称" align="center" prop="groupName" width="120"/>
      <el-table-column label="商户" align="center" prop="shopName" width="120"/>
      <el-table-column label="备注" align="center" prop="remark" width="200"/>
      <el-table-column
        label="操作"
        align="center"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resource:group:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resource:group:remove']"
          >删除
          </el-button>
        </template>

      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
<!--    查询资源分组-->
    <el-dialog :title="title" :visible.sync="groupOpen" width="800px" append-to-body>
      <el-row :gutter="10" class="mb8" >
        <el-col :span="1.5">
          <el-button

            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['resource:group:add']"
          >新增
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
        <el-table-column label="商户" align="center" prop="shopName" width="120"/>
        <el-table-column label="分组名称" align="center" prop="groupName" width="120"/>
        <el-table-column label="备注" align="center" prop="remark" width="200"/>
        <el-table-column
          label="操作"
          align="center"
          width="300"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">

            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['resource:group:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['resource:group:del']"
            >删除
            </el-button>
          </template>

        </el-table-column>

      </el-table>
      <pagination
        v-show="groupTotal > 0"
        :total="groupTotal"
        :page.sync="queryGroup.pageNum"
        :limit.sync="queryGroup.pageSize"
        @pagination="getGroupList"
      />
    </el-dialog>

    <!-- 添加或修改分组配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="组名" prop="groupName">
          <el-input v-model="form.groupName"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { list, groupAdd, groupEdit, groupDel, info } from '@/api/resource/class'
import { mapState } from 'vuex'
import { listUser } from '@/api/merchant/merchant'

export default {
  name: 'class',
  data() {
    return {
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否显示分组列表弹出层

      groupOpen:false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      //分组资源查询参数
      queryGroup:{
        pageNum: 1,
        pageSize: 10
      },
      //查看分组弹窗表格
      groupList:[],

      dateRange: [],
      statusOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //商户id（用于新增操作）
      shopID:"",
      //分组总数
      groupTotal:0

    }
  },
  computed: {
    ...mapState({
      rolesKey: state => state.user.rolesKey
    })
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
      this.statusOptions.forEach(item =>{
        item.dictValue = Number(item.dictValue)
      })
    });
  },
  methods: {
    getList() {
      this.loading = true
      if (this.rolesKey == '00') {
        listUser(this.queryParams).then(response => {
            this.list = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      }else {
        list(this.queryParams).then(
          response => {
            this.list = response.rows
            this.total = response.total
            this.loading = false
          }
        )
      }
    },
    getGroupList(){
      list(this.queryGroup).then(
        response => {
          this.groupList = response.rows
          this.groupTotal = response.total
          this.loading = false
        }
      )
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      console.log(this.ids)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.open = true
      this.title = '添加分组'
      this.form.shopId = row.id || this.shopID
    },
    /** 查看商户资源分组操作 */
    handleList(row) {
      this.reset()
      this.groupOpen = true
      this.title = '资源分组列表'
      this.queryGroup.shopId = row.id;
      this.shopID = row.id;
      this.getGroupList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids;
      info(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改分组'
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            groupEdit(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              if (this.rolesKey == '00') {
                this.getGroupList()

              }else {
                this.getList()

              }
            })
          } else {
            groupAdd(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false;
              this.merchantOpen = false;
              if (this.rolesKey == '00') {
                this.getGroupList()

              }else {
                this.getList()

              }
              this.reset()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const couponId = row.id || this.ids.toString()
      this.$confirm('是否确认删除分组为"' + couponId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return groupDel(couponId)
      }).then(() => {
        list({
          pageNum: 1,
          pageSize: 10,
          shopId: row.shopId
        }).then(
          response => {
            this.groupList = response.rows
            this.total = response.total
            this.loading = false
            this.getList();
          }
        )
        this.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }

}
</script>

<style scoped>

</style>
